<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/23 0023
 * Time: 09:59
 */

namespace app\currency\controller;


use library\Controller;
use think\Db;
use think\exception\HttpResponseException;

/**
 * 自定义地址申请
 * Class CitiesMemberCustom
 * @package app\currency\controller
 */
class CitiesMemberCustom extends Controller
{
    protected $table = 'CitiesMemberCustom';

    /**
     * 自定义地址列表
     * @menu true
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->_query($this->table)
            ->equal('status')
            ->dateBetween('create_at')
            ->order('status asc,create_at desc')
            ->page();
    }
    protected function _index_page_filter(array &$data)
    {
        $mids = array_unique(array_merge(array_column($data, 'mid')));
        $memberList = Db::name('Member')->whereIn('id', $mids)->select();
        foreach ($data as &$vo) {
            $vo['member'] = [];
            foreach ($memberList as $member) if ($member['id'] === $vo['mid']) {
                $vo['member'] = $member;
            }
        }
    }

    /**
     * 编辑地址
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->_form($this->table,'form');
    }
    protected function _form_filter(&$data)
    {
        if ($this->request->isPost())
        {
            empty($data['city_id']) && $this->error('请选择地址！');
            //检测所选地址是否有下级地址
            if(Db::name('Cities')->where('pid','eq',$data['city_id'])->count() > 0)
            {
                $this->error('请选择最底层地址！');
            }
            //插入用户地址表
            try {
                Db::transaction(function() use($data) {
                    Db::name('CitiesMember')->insert([
                        'mid' => $data['mid'],
                        'cities_id' => $data['city_id'],
                        'contact_name' => $data['contact_name'],
                        'contact_phone' => $data['contact_phone']
                    ]);
                });
                Db::name($this->table)->where('id',$data['id'])->setField('status','1');
                $this->success('操作成功！', '');
            } catch (HttpResponseException $exception) {
                throw $exception;
            } catch (\Exception $e) {
                $this->error("操作失败！{$e->getMessage()}");
            }
        }
    }

    /**
     * 删除
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }
}